package englishChicken;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Map;

import utils.UIDGenerator;
import data.JDBCEventoDAO;

public class AlmacenDeEventos {

	private static AlmacenDeEventos ae;
	private static JDBCEventoDAO jdbcEventos;
	
	public static synchronized AlmacenDeEventos getInstance(){
		if(ae==null){
			ae=new AlmacenDeEventos();
			jdbcEventos=new JDBCEventoDAO();
		}
		return ae;
	}
	
	private AlmacenDeEventos(){
	}
	
	public void actualizarEvento(Evento e){
		jdbcEventos.update(e);
	}
	
	public void addEvento(Evento e){
		UIDGenerator uidG=UIDGenerator.getInstance();
		jdbcEventos.insert(uidG.getKey(),e);
	}
	
	public List<Evento> getEventos() {
		List<Evento> eventos=jdbcEventos.selectAll();
		Iterator<Evento> eIT=eventos.iterator();
		List<Evento> peticion=new LinkedList<Evento>();
		while(eIT.hasNext()){
			Evento eAct=eIT.next();
			peticion.add(eAct);
		}
		return peticion;
	}

	public Evento getEvento(String nombre) {
		String caracteresEspeciales=
			"Ã¡:&aacute;,"+
			"Ã©:&eacute;,"+
			"Ã­:&iacute;,"+
			"Ã³:&oacute;,"+
			"Ãº:&uacute;,"+
			"Ã±:&ntilde;,"+
			"Ã:&Aacute;,"+
			"Ã:&Eacute;,"+
			"Ã:&Iacute;,"+
			"Ã:&Oacute;,"+
			"Ã:&Uacute;,"+
			"Ã:&Ntilde";
		String cEspArray[]=caracteresEspeciales.split(",");
		for(int i=0;i<cEspArray.length;i++)
			nombre=nombre.replaceAll(cEspArray[i].split(":")[0],cEspArray[i].split(":")[1]);
		return jdbcEventos.selectByNombre(nombre);
	}
	
	public Evento getEventoUid(String uid){
		return jdbcEventos.selectByEID(uid);
	}
	
	public void borraEvento (String eUid){
		jdbcEventos.delete(eUid);
	}

	public Integer obtenerAsistentesMaximos(){
		return jdbcEventos.getMaxAsistentes();
	}
	
	public String obtenerMinInicio(){
		return jdbcEventos.getMinInicio();
	}
	
	public String obtenerMaxFin(){
		return jdbcEventos.getMaxFin();
	}
	
	public Map<String,String> buscar(Map<String,String> p1,Map<String,String[]>p2){
		return jdbcEventos.buscar(p1,p2);
	}
}